core: Add API to convert checksum -> csum in place
authorColin Walters <walters@verbum.org>
Wed, 14 Aug 2013 22:17:09 +0000 (18:17 -0400)
committerColin Walters <walters@verbum.org>
Wed, 14 Aug 2013 23:49:54 +0000 (19:49 -0400)
We already have the opposite, and this will be used in some
places to avoid a malloc.

https://bugzilla.gnome.org/show_bug.cgi?id=706031

src/libostree/ostree-core.c
src/libostree/ostree-core.h

index f1e1fc97f4a477b5bf9d3ebc3a6fb5c7d578aad5..92311418283dd6cdbd2eab145e5e58e01f953f2f 100644 (file)
@@ -972,9 +972,17 @@ ostree_object_name_deserialize (GVariant         *variant,
   *out_objtype = (OstreeObjectType)objtype_u32;
 }
 
-static void
-checksum_to_bytes (const char *checksum,
-                   guchar     *buf)
+/**
+ * ostree_checksum_inplace_to_bytes:
+ * @checksum: a SHA256 string
+ * @buf: Output buffer with at least 32 bytes of space
+ *
+ * Convert @checksum from a string to binary in-place, without
+ * allocating memory.  Use this function in hot code paths.
+ */
+void
+ostree_checksum_inplace_to_bytes (const char *checksum,
+                                  guchar     *buf)
 {
   guint i;
   guint j;
@@ -1000,7 +1008,7 @@ guchar *
 ostree_checksum_to_bytes (const char *checksum)
 {
   guchar *ret = g_malloc (32);
-  checksum_to_bytes (checksum, ret);
+  ostree_checksum_inplace_to_bytes (checksum, ret);
   return ret;
 }
 
@@ -1008,7 +1016,7 @@ GVariant *
 ostree_checksum_to_bytes_v (const char *checksum)
 {
   guchar result[32];
-  checksum_to_bytes (checksum, result);
+  ostree_checksum_inplace_to_bytes (checksum, result);
   return ot_gvariant_new_bytearray ((guchar*)result, 32);
 }
 
index 6f892659ac5bb7eade2cdc05dbd867cc4dc63796..a28b0ca8a8f5c2271ded0c4448246e258929b632 100644 (file)
@@ -132,6 +132,9 @@ char * ostree_checksum_from_bytes_v (GVariant *bytes);
 void ostree_checksum_inplace_from_bytes (const guchar *bytes,
                                          char         *buf);
 
+void ostree_checksum_inplace_to_bytes (const char *checksum,
+                                       guchar     *buf);
+
 const guchar *ostree_checksum_bytes_peek (GVariant *bytes);
 
 int ostree_cmp_checksum_bytes (const guchar *a, const guchar *b);